package com.health.mapp.impl;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;

import com.health.db.model.UserCardIssue;
import com.health.db.model.UserCardType;
import com.health.db.service.user.IUserCardListService;
import com.health.db.service.user.IUserLISTService;
import com.health.mapp.bean.ActiveCardReq;
import com.health.mapp.bean.ActiveCardResp;
import com.health.mapp.bean.Header;
import com.health.mapp.utils.Constant;
import com.health.mapp.utils.Response;
import com.health.mapp.utils.ResultCode;

/**
 * 卡激活接口
 * 充值卡 健康卡 会员卡
 * @author HiRoad
 *
 */
public class ActiveCardImpl  implements ActionInterface {

	private static Logger logger = Logger.getLogger(ActiveCardImpl.class);
	
	private IUserLISTService userLISTService;
	private IUserCardListService userCardListService;
	
	@Override
	public Response execute(Header header, Object reqBean) throws Exception {
		
		ActiveCardReq req=(ActiveCardReq) reqBean;
		ActiveCardResp resp = new ActiveCardResp();
		//2为充值卡
		String channel = req.getChannel();
		String lable="";
		if (StringUtils.isNotBlank(channel)&&("2".equals(channel))) {
			lable ="充值";
		}else if(("1".equals(channel))){
			lable ="健康";
		}else{
			lable ="会员";
		}
		
		
		if(StringUtils.isBlank(req.getUserId())||StringUtils.isBlank(req.getCardCode())){
			resp.setErrorCode(ResultCode.OPERATION_LAKE_PARAMETER_ERROR);
			resp.setErrorMsg(ResultCode.OPERATION_LAKE_PARAMETER_ERROR_NOTE);
			return resp;
		}
		logger.info("卡号:"+req.getCardCode()+";卡号验证");
		UserCardIssue issue = userCardListService.selectUserCardIssueByPasword(req.getCardCode());
		
		if (null==issue) {
			resp.setErrorCode(ResultCode.OPERATION_ACTIVE_ERROR_3009);
			resp.setErrorMsg(lable+ResultCode.OPERATION_ACTIVE_ERROR_3009_NOTE);
			return resp;
		}else if (null!=issue.getOwnerid()) {
			resp.setErrorCode(ResultCode.OPERATION_ACTIVE_ERROR_3011);
			resp.setErrorMsg(lable+ResultCode.OPERATION_ACTIVE_ERROR_3011_NOTE);
			return resp;
		}else if (null==issue.getUserCardType()) {
			resp.setErrorCode(ResultCode.OPERATION_ACTIVE_ERROR_3010);
			resp.setErrorMsg(lable+ResultCode.OPERATION_ACTIVE_ERROR_3010_NOTE);
			return resp;
		}
		
		UserCardType userCardType = issue.getUserCardType();
		
		//会员卡激活
		logger.info("卡号:"+req.getCardCode()+"会员卡激活");
		if (null!=userCardType.getGoodsTypeId()&&(userCardType.getGoodsTypeId().intValue()==Constant.CONSTANTS_PAY_ACTION_ID_51.intValue())) {
			userCardListService.activeVIPUserCard(Long.valueOf(req.getUserId()), issue);
		}else{
			userCardListService.activeUserCardByPasword(Long.valueOf(req.getUserId()), issue);
			
			resp.setLeftTime01(((userCardType.getCardtime01())==null?"0":String.valueOf(userCardType.getCardtime01().setScale(0)))+"次");
			resp.setLeftTime02(((userCardType.getCardtime02())==null?"0":String.valueOf(userCardType.getCardtime02().setScale(2)))+"元");
			resp.setLeftTime03(((userCardType.getCardtime03())==null?"0":String.valueOf(userCardType.getCardtime03().setScale(0)))+"秒");
			resp.setLeftTime04(((userCardType.getCardtime04())==null?"0":String.valueOf(userCardType.getCardtime04().setScale(0)))+"次");
			resp.setLeftTime05(((userCardType.getCardtime05())==null?"0":String.valueOf(userCardType.getCardtime05().setScale(0)))+"次");
		}
		resp.setPartnerCode(issue.getComment());
		
		//ACTIVE 激活 默认
		String url="image/TK_01.png";
		resp.setPartnerUrl(url.trim());
		/*if (StringUtils.isNotBlank(issue.getComment())) {
			String url = PropertiesService.getString(issue.getComment());
			
			if (StringUtils.isNotBlank(url)) {
				resp.setPartnerUrl(url.trim());
			}
		}*/
	
		
		resp.setErrorCode(ResultCode.OPERATION_SUCCESS);
		resp.setErrorMsg(lable+ResultCode.OPERATION_ACTIVE__3012_NOTE);
		
		return resp;
	}
	public IUserLISTService getUserLISTService() {
		return userLISTService;
	}
	public void setUserLISTService(IUserLISTService userLISTService) {
		this.userLISTService = userLISTService;
	}
	/**
	 * @return the userCardListService
	 */
	public IUserCardListService getUserCardListService() {
		return userCardListService;
	}
	/**
	 * @param userCardListService the userCardListService to set
	 */
	public void setUserCardListService(IUserCardListService userCardListService) {
		this.userCardListService = userCardListService;
	}
	
	
	
	
	
}
